from pyecharts.charts import Page
import pandas as pd
import sys
sys.path.append("..//database")
from SqlWizard import *
from CmnFuncs import *

def run_poly():
    '''
    本模块包含品种：PTA, MEG, EB， 关联品种：原油、棉花
    设计图表：
    1. PTA现货基差表；
    2. PTA月差表；
    3. EG无现货数据
    4. EG月差表；
    5. EB现货基差表；
    6. EB月差表；
    7. TA / EG 现货价差，各月合约价差
    8。 EG/ EB 月合约价差
    9. TA主力与原油主连比价
    '''

    a_list = date_checker_1()

    sc_main = read_sql_fut('fut_scl')

    ta_spot = read_sql_spot('spot_ta')
    ta_index = read_sql_fut_index('fut_index_ta')
    ta_ry = read_sql_ry('roll_yield_ta')
    ta_spdrdx = read_sql_spdrdx('spdrdex_ta')
    ta_1 = read_sql_fut('fut_ta{}'.format(a_list[0]))
    ta_2 = read_sql_fut('fut_ta{}'.format(a_list[1]))
    ta_3 = read_sql_fut('fut_ta{}'.format(a_list[2]))

    eg_spot = read_sql_spot('spot_eg')
    eg_index = read_sql_fut_index('fut_index_eg')
    eg_ry = read_sql_ry('roll_yield_eg')
    eg_spdrdx = read_sql_spdrdx('spdrdex_eg')
    eg_1 = read_sql_fut('fut_eg{}'.format(a_list[0]))
    eg_2 = read_sql_fut('fut_eg{}'.format(a_list[1]))
    eg_3 = read_sql_fut('fut_eg{}'.format(a_list[2]))

    eb_spot = read_sql_spot('spot_eb')
    eb_index = read_sql_fut_index('fut_index_eb')
    eb_ry = read_sql_ry('roll_yield_eb')
    eb_spdrdx = read_sql_spdrdx('spdrdex_eb')
    eb_1 = read_sql_fut('fut_eb{}'.format(a_list[0]))
    eb_2 = read_sql_fut('fut_eb{}'.format(a_list[1]))
    eb_3 = read_sql_fut('fut_eb{}'.format(a_list[2]))

    # PTA
    header_ta = draw_separator('PTA')
    # PTA远期曲线
    future_curve_ta = draw_future_curve(ta_spot, ta_1, ta_2, ta_3)
    # TA一致性预期指标
    idctr_ta = draw_spec_idctr('TA', ta_spdrdx, ta_index, ta_ry)
    # 1. PTA现货基差表；
    ta_spot_chart = draw_spot_spd('PTA', ta_spot)
    # PTA合约价格、成交量、持仓量、持仓量变化图
    ta_contract_1 = draw_contract('ta{}'.format(a_list[0]), ta_1)
    ta_contract_2 = draw_contract('ta{}'.format(a_list[1]), ta_2)
    ta_contract_3 = draw_contract('ta{}'.format(a_list[2]), ta_3)
    # 2. PTA月差表；
    ta_12_chart = draw_spd('ta{}'.format(a_list[0]), ta_1['close'], 'ta{}'.format(a_list[1]), ta_2['close'])
    ta_23_chart = draw_spd('ta{}'.format(a_list[1]), ta_2['close'], 'ta{}'.format(a_list[2]), ta_3['close'])

    # MEG
    header_eg = draw_separator('MEG')
    # eg future curve
    future_curve_eg = draw_future_curve(eg_spot, eg_1, eg_2, eg_3)
    # EG一致性预期指标
    idctr_eg = draw_spec_idctr('EG', eg_spdrdx, eg_index, eg_ry)
    # 5. EG现货基差表；
    eg_spot_chart = draw_spot_spd('EG', eg_spot)
    # MEG合约价格、成交量、持仓量、持仓量变化图
    eg_contract_1 = draw_contract('eg{}'.format(a_list[0]), eg_1)
    eg_contract_2 = draw_contract('eg{}'.format(a_list[1]), eg_2)
    eg_contract_3 = draw_contract('eg{}'.format(a_list[2]), eg_3)
    # 4. EG月差表；
    eg_12_chart = draw_spd('eg{}'.format(a_list[0]), eg_1['close'], 'eg{}'.format(a_list[1]), eg_2['close'])
    eg_23_chart = draw_spd('eg{}'.format(a_list[1]), eg_2['close'], 'eg{}'.format(a_list[2]), eg_3['close'])

    # EB
    header_eb = draw_separator('EB')
    # EB远期曲线
    future_curve_eb = draw_future_curve(eb_spot, eb_1, eb_2, eb_3)
    # EB一致性预期指标
    idctr_eb = draw_spec_idctr('EB', eb_spdrdx, eb_index, eb_ry)
    # 5. EB现货基差表；
    eb_spot_chart = draw_spot_spd('EB', eb_spot)
    # EB合约价格、成交量、持仓量、持仓量变化图
    eb_contract_1 = draw_contract('eb{}'.format(a_list[0]), eb_1)
    eb_contract_2 = draw_contract('eb{}'.format(a_list[1]), eb_2)
    eb_contract_3 = draw_contract('eb{}'.format(a_list[2]), eb_3)
    # 6. EB月差表；
    eb_12_chart = draw_spd('eb{}'.format(a_list[0]), eb_1['close'], 'eb{}'.format(a_list[1]), eb_2['close'])
    eb_23_chart = draw_spd('eb{}'.format(a_list[1]), eb_2['close'], 'eb{}'.format(a_list[2]), eb_3['close'])

    # 品种间
    header_cross = draw_separator('品种间')
    # 7. TA / EG 各月合约价差
    arbi_record('ta', ta_index['close'], 'eg', eg_index['close'], x=1, y=1, opt=2, cat=1)
    arbi_record('ta', ta_spot['spot'], 'eg', eg_spot['spot'], x=1, y=1, opt=2, cat=2)
    dif_ta_eg_spot = draw_price_dif('TA现货', ta_spot['spot'], 'EG现货', eg_spot['spot'], 3)
    dif_ta_eg_main = draw_price_dif('TA主连', ta_spot['main_price'], 'EG主连', eg_spot['main_price'], 3)
    dif_ta_eg_1 = draw_price_dif('ta{}'.format(a_list[0]), ta_1['close'], 'eg{}'.format(a_list[0]), eg_1['close'], 3)
    dif_ta_eg_2 = draw_price_dif('ta{}'.format(a_list[1]), ta_2['close'], 'eg{}'.format(a_list[1]), eg_2['close'], 3)
    dif_ta_eg_3 = draw_price_dif('ta{}'.format(a_list[2]), ta_3['close'], 'eg{}'.format(a_list[2]), eg_3['close'], 3)
    # 8. EG/ EB 各月合约价差
    arbi_record('eg', eg_index['close'], 'eb', eb_index['close'], x=1, y=1, opt=2, cat=1)
    arbi_record('eg', eg_spot['spot'], 'eb', eb_spot['spot'], x=1, y=1, opt=2, cat=2)
    dif_eg_eb_spot = draw_price_dif('EG现货', eg_spot['spot'], 'EB现货', eb_spot['spot'], 3)
    dif_eg_eb_main = draw_price_dif('EG主连', eg_spot['main_price'], 'EB主连', eb_spot['main_price'], 3)
    dif_eg_eb_1 = draw_price_dif('eg{}'.format(a_list[0]), eg_1['close'], 'eb{}'.format(a_list[0]), eb_1['close'])
    dif_eg_eb_2 = draw_price_dif('eg{}'.format(a_list[1]), eg_2['close'], 'eb{}'.format(a_list[1]), eb_2['close'])
    dif_eg_eb_3 = draw_price_dif('eg{}'.format(a_list[2]), eg_3['close'], 'eb{}'.format(a_list[2]), eb_3['close'])
    # 9. TA主力与原油主连比价
    ratio_ta_sc = draw_price_ratio('ta{}'.format(a_list[0]), ta_1['close'], '原油主连', sc_main['close'], 1)

    page = Page(page_title='聚酯链', layout=Page.SimplePageLayout)
    page.add(
        header_ta,
        future_curve_ta,
        idctr_ta,
        ta_spot_chart,
        ta_contract_1,
        ta_contract_2,
        ta_contract_3,
        ta_12_chart,
        ta_23_chart,
        header_eg,
        future_curve_eg,
        idctr_eg,
        eg_spot_chart,
        eg_contract_1,
        eg_contract_2,
        eg_contract_3,
        eg_12_chart,
        eg_23_chart,
        header_eb,
        future_curve_eb,
        idctr_eb,
        eb_spot_chart,
        eb_contract_1,
        eb_contract_2,
        eb_contract_3,
        eb_12_chart,
        eb_23_chart,
        header_cross,
        dif_ta_eg_spot,
        dif_ta_eg_main,
        dif_ta_eg_1,
        dif_ta_eg_2,
        dif_ta_eg_3,
        dif_eg_eb_spot,
        dif_eg_eb_main,
        dif_eg_eb_1,
        dif_eg_eb_2,
        dif_eg_eb_3,
        ratio_ta_sc,
    )
    page.render('聚酯链.html')
